package com.example.softpart.satokenFunction;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.lang.Console;
import com.example.softpart.unil.callMagicAPi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.ssssssss.magicapi.core.service.MagicAPIService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 自定义权限验证接口扩展
 */
@Component    // 保证此类被SpringBoot扫描，完成Sa-Token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private MagicAPIService service;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {

        HashMap map = new HashMap();
        map.put("user_id", loginId);

        return getAccessList(getUserRoleList(map));
    }


    //获取用户权限列表
    public ArrayList<String> getAccessList(List roleList) {
        ArrayList list = new ArrayList();
        if (roleList.size() > 0) {
            for (Object role : roleList) {
                HashMap map = new HashMap();
                map.put("role_name", role);

                List accessList = callMagicAPi.passCall(service, map, "GET", "/role_access/role_access");
                if (accessList.size() > 0) {
                    for (Object o : accessList) {
                        HashMap m = (HashMap) o;
                        list.add(m.get("accessUrls") + "/" + m.get("accessMethod"));
                    }
                }
            }
        }

        return list;
    }


    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        HashMap map = new HashMap();
        map.put("user_id", loginId);
        return getUserRoleList(map);
    }


    //获取该用户的角色列表
    public ArrayList getUserRoleList(HashMap map) {

        Console.log("获取用户的角色列表");
        //获取用户的角色列表
        ArrayList list = new ArrayList();

        List roleList = callMagicAPi.passCall(service, map, "GET", "/user_role/user_role");
        if (roleList.size() > 0) {
            for (Object o : roleList) {
                HashMap m = (HashMap) o;
                list.add(m.get("roleName"));
            }
        }


        //获取临时角色
        List temporaryRoleList = callMagicAPi.passCall(service, map, "GET", "/temporary_role/temporary_role");
        if (temporaryRoleList.size() > 0) {
            for (Object o1 : temporaryRoleList) {
                HashMap map1 = (HashMap) o1;
                list.add(map1.get("roleName"));
            }
        }

        Console.log(list.toString());
        return list;
    }


    //获取该用户权限列表


    @RestControllerAdvice
    public class GlobalExceptionHandler {
        // 全局异常拦截
        @ExceptionHandler
        public SaResult handlerException(Exception e) {
            e.printStackTrace();
            return SaResult.error(e.getMessage());
        }
    }


}